package com.snap.vseries.analyze;

import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;

/**
 * y=ax+b  x为对浓度取log
 */
public class StandardCurveFit {

    public static float[] getLogArray(float[] concentrationArray) {
        float[] array = new float[concentrationArray.length];
        for (int i = 0; i < concentrationArray.length; i++) {
            array[i] = (float) Math.log10(concentrationArray[i]);
        }
        return array;
    }

    public static void fit(float[] concentrationArray, float[] ctArray) {
        //todo 浓度数组中 不相同的值<3  及时return
        float[] logArray = getLogArray(concentrationArray);
        WeightedObservedPoints points = new WeightedObservedPoints();
        for (int x = 0; x < logArray.length; x++) {
            points.add(logArray[x], ctArray[x]);
        }
        PolynomialCurveFitter fitter = PolynomialCurveFitter.create(1);
        double[] params = fitter.fit(points.toList());
    }

    public float getCopy(float a1, float a0, float ctValue) {
        float concentrationLog10 = (ctValue - a0) / a1;
        return (float) Math.pow(10, concentrationLog10);
    }
}
